package com.esp.soccertutor.ui.xmlparser;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esp.soccertutor.ui.bean.BeanCategory;
import com.esp.soccertutor.util.Log;

public class XMLParser_Category {
	
	
	public static String TAG = XMLParser_Category.class.getCanonicalName();
	
	private ArrayList<BeanCategory> arrylstCategory;
	private String strServerXML;
	private static Document docCategory;
	public static int STATUS_CODE;
	public static int STATUS_VALID_CID = -1;
	public static int NODE_COUNT;
	public static String MESSAGE;
	
	
	/**
	 * THIS IS HERE FOR HTTP POST URL
	 * @param wsURL
	 * @param params
	 */
	
	public XMLParser_Category(String wsURL,ArrayList<BasicNameValuePair> params){
		
		try
		{
			XMLDocument.getHttpPostXML(wsURL, params);
			strServerXML = "<response><code>1</code><message>content categories</message><count>4</count><categories>"
					+ "<category><id>1</id><title>Youth Coaching</title></category><category><id>2</id><title>Passing and Possession</title></category>"
					+ "<category><id>3</id><title>Planners/Notebooks</title></category><category><id>4</id><title>Skills and Techniques</title></category>"
					+ "</categories></response>";
			
			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docCategory = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
	}
	/**
	 * THIS IS HERE FOR HTTP GET URL
	 * @param wsURL
	 */
	public XMLParser_Category(String wsURL){
		
		try
		{
			strServerXML = new String();
			XMLDocument.getHttpGetXML(wsURL);
			strServerXML = "<response><code>1</code><message>content categories</message><count>4</count><categories>"
					+ "<category><id>1</id><title>Youth Coaching</title></category><category><id>2</id><title>Passing and Possession</title></category>"
					+ "<category><id>3</id><title>Planners/Notebooks</title></category><category><id>4</id><title>Skills and Techniques</title></category>"
					+ "</categories></response>";
			
			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docCategory = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
	}
	
	/**
	 * THIS CALLBACK WHEN WE REACHED DOCUMENT
	 * @return
	 */
	public static Document GetDocument(){
		
		if(docCategory != null){
			return docCategory;
		}else{
			return null;
		}
	}
	
	public ArrayList<BeanCategory> getCategory(Document docCategory){
		try
		{
			if(docCategory != null)
				return docBuild(docCategory);
			else
				return null;
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	private ArrayList<BeanCategory> docBuild(Document doc) throws Exception{
		
		if(doc != null)
		{

			
			
			
			Element elem$root = doc.getDocumentElement();
			NodeList nl$item = elem$root.getElementsByTagName("code");
			Element  line  = (Element)nl$item.item(0);
			STATUS_VALID_CID = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line, "code"));
			
			NodeList nodeMessage = elem$root.getElementsByTagName("message");
			line  = (Element)nodeMessage.item(0);
			MESSAGE = XMLDocument.getCharacterDataFromElement(line, "message");
			
			NodeList nodeCount = elem$root.getElementsByTagName("count");
			line = (Element)nodeCount.item(0);
			NODE_COUNT = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line,"count"));
				
			
			NodeList nodeCategory = elem$root.getElementsByTagName("category");
			
			if(STATUS_VALID_CID == 1)
			{
				arrylstCategory = new ArrayList<BeanCategory>();
				for (int i = 0; i < nodeCategory.getLength(); i++) 
					{
						
						BeanCategory mBeanCate = new BeanCategory();
						
						Element elementPos= (Element) nodeCategory.item(i);
						
						NodeList nodeID = elementPos.getElementsByTagName("id");
						line  = (Element)nodeID.item(0);
						mBeanCate.setId(XMLDocument.getCharacterDataFromElement(line, "id"));
						
						
						NodeList nodeTitle = elementPos.getElementsByTagName("title");
						line  = (Element)nodeTitle.item(0);
						mBeanCate.setTitle(XMLDocument.getCharacterDataFromElement(line, "title"));
						
						arrylstCategory.add(mBeanCate);
					
					}
			}
			
		}
		return arrylstCategory;
	}
	
	
}
